#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
const int N = 3e4 + 10;
int a[N];
int d[N];
int c[N];
int n = 3e4;
int find(int x)
{
    if (a[x] == x)
    {
        return x;
    }
    int u = find(a[x]);
    d[x] = d[a[x]] + d[x];
    return a[x] = u;
}
void un(int x, int y)
{
    int fx = find(x);
    int fy = find(y);
    if (fx != fy)
    {
        a[fx] = fy;
        d[fx] = c[fy];
        c[fy] += c[fx];
    }
}
int que(int x, int y)
{
    int fx = find(x);
    int fy = find(y);
    if (fx != fy)
    {
        return -1;
    }
    else
    {
        return abs(d[y] - d[x]) - 1;
    }
}
int main()
{
    for (int i = 1; i <= n; i++)
    {
        a[i] = i;
        c[i] = 1;
    }
    int t;
    cin >> t;
    while (t--)
    {
        char op;
        int x, y;
        cin >> op >> x >> y;
        if (op == 'M')
        {
            un(x, y);
        }
        else
        {
            cout << que(x, y) << endl;
        }

    }
    return 0;
}